import 'package:flutter/material.dart';
import 'package:hxdj/message/rongcloud/chat/rc_chat_base_input.dart';
import 'package:hxdj/message/rongcloud/rc_chat/controller/rc_chat_base_input_controller.dart';
import 'package:hxdj/message/rongcloud/rc_chat/controller/rc_chat_controller.dart';

class ChatInputWidget extends StatefulWidget {
  //输入框控制器
  final RCChatInputTextFieldController textFieldController;

  //聊天控制器
  final RCChatController chatController;

  const ChatInputWidget({super.key, required this.textFieldController, required this.chatController});

  @override
  State<ChatInputWidget> createState() => _ChatInputWidgetState();
}

class _ChatInputWidgetState extends State<ChatInputWidget> {
//
  late TextEditingController textEditingController = TextEditingController();

  late FocusNode focusNode = FocusNode();

  @override
  void initState() {
    super.initState();
    widget.textFieldController.addListener(controllerHandler);
  }

//控制器事件
  controllerHandler() {
    final MyTextFieldActionType? actionType = widget.textFieldController.actionType;

    if (actionType == MyTextFieldActionType.setTextField) {
      final newText = widget.textFieldController.inputText;
      textEditingController.text = newText;
      textEditingController.selection =
          TextSelection.fromPosition(TextPosition(offset: textEditingController.text.length));
      focusNode.requestFocus();
      return;
    } else if (actionType == MyTextFieldActionType.requestFocus) {
      focusNode.requestFocus();
      return;
    }
  }

  @override
  void dispose() {
    widget.textFieldController.removeListener(controllerHandler);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
      return MyBaseChatInputWidget(
        onSendText: (value) {
          if (textEditingController.text.isEmpty) return;

          



          //发送消息
          widget.chatController.sendMessage(context, widget.chatController.model!.targetId, textEditingController.text);
        },
        textEditingController: textEditingController,
        focusNode: focusNode,
        textFieldController: widget.textFieldController,
      );
    });
  }
}


/**
 *  Column(
      children: [
        ConstrainedBox(
          constraints: const BoxConstraints(
            maxHeight: 100.0, // 设置最大高度为 100
          ),
          child: Container(
            decoration: const BoxDecoration(color: MyColours.bgColor, boxShadow: <BoxShadow>[
              BoxShadow(
                color: MyColours.color2E2626,
                offset: Offset(0.0, -13),
                blurRadius: 12.0,
                spreadRadius: -9.0,
              )
            ]),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Expanded(
                  child: Container(
                    margin: EdgeInsets.only(left: 15.w, right: 15.w, top: 5.h, bottom: 5.h),
                    decoration: BoxDecoration(
                        color: MyColours.materialBg,
                        //设置四周圆角 角度
                        borderRadius: BorderRadius.circular(8)),
                    child: MyKeyboardVisibility(
                      onChanged: (bool visibility) {
                        if (isShowKeyboard != visibility) {
                          setState(() {
                            isShowKeyboard = visibility;
                          });
                        }
                      },
                      child: TextField(
                        focusNode: focusNode,
                        controller: textEditingController,
                        maxLines: null,
                        decoration: const InputDecoration(
                          hintText: "请输入消息...",
                          border: InputBorder.none, // 隐藏下划线
                          contentPadding: EdgeInsets.fromLTRB(15, 0.0, 10.0, 10.0), // 内容内边距
                          focusedBorder:
                              OutlineInputBorder(borderSide: BorderSide(width: 0, color: Colors.transparent)),
                          disabledBorder:
                              OutlineInputBorder(borderSide: BorderSide(width: 0, color: Colors.transparent)),
                          enabledBorder:
                              OutlineInputBorder(borderSide: BorderSide(width: 0, color: Colors.transparent)),
                        ),
                      ),
                    ),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(right: 10.w, top: 12.h),
                  child: SizedBox(
                    width: 60,
                    height: 30,
                    child: ElevatedButton(
                      onPressed: () {
                        //发送消息给对方
                        debugPrint("发送消息给对方 ${widget.conversation?.targetId}");
                        if (textEditingController.text.isNotEmpty) {
                          debugPrint("textEditingController.text =  ${textEditingController.text}");

                          RCEngineManager().sendTextMessage(
                              widget.conversation!.targetId ?? "", textEditingController.text,
                              callback: RCIMIWSendMessageCallback(onMessageSent: (int? code, RCIMIWMessage? message) {
                                debugPrint("code.text =  ${code}");
                                debugPrint("message.text =  ${message!.receivedStatus}");
                              }, onMessageSaved: (RCIMIWMessage? message) {
                                //更新最近的会话
                                Provider.of<MessageViewModel>(context, listen: false)
                                    .updateRecentConversationLis(widget.conversation!.targetId!);

                                //插入消息
                                Provider.of<RCChatViewModel>(context, listen: false).insertMessage(message!);
                                textEditingController.clear();
                              }));
                        }
                      },
                      style: ButtonStyle(
                          padding: const MaterialStatePropertyAll(EdgeInsets.zero),
                          shape: MaterialStatePropertyAll(
                            RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(3),
                            ),
                          ),
                          backgroundColor: const MaterialStatePropertyAll(MyColours.appMain)),
                      child: const Text(
                        "发送",
                        style: TextStyle(color: Colors.white, fontSize: 16),
                      ),
                    ),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(right: 10.w, top: 12.h),
                  child: SizedBox(
                    width: 60,
                    height: 30,
                    child: ElevatedButton(
                      onPressed: () {
                        if (!isShowMore) {
                          //失去焦点
                          focusNode.unfocus();
                        }

                        setState(() {
                          isShowKeyboard = false;
                          isShowMore = !isShowMore;
                        });
                      },
                    
                    
                      style: ButtonStyle(
                          padding: const MaterialStatePropertyAll(EdgeInsets.zero),
                          shape: MaterialStatePropertyAll(
                            RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(3),
                            ),
                          ),
                          backgroundColor: const MaterialStatePropertyAll(MyColours.appMain)),
                      child: const Text(
                        "文件",
                        style: TextStyle(color: Colors.white, fontSize: 16),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
        Container(
          height: bottomPadding,
          color: MyColours.bgColor,
        ),
        AnimatedContainer(
          duration: Duration(milliseconds: 340),
          curve: Curves.fastOutSlowIn,
          height: max(getBottomHeight(), 0.0),
          child: ListView(
            physics: const NeverScrollableScrollPhysics(),
            children: [
              Container(
                child: MyText("行三"),
                color: MyColours.appMain,
              )
            ],
          ),
        ),
      ],
    );
 */